package ar.com.ford.it.fullEconomics.view.form;

import java.util.Calendar;
import java.util.Collection;
import java.util.GregorianCalendar;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.util.MessageResources;
import org.apache.struts.validator.ValidatorForm;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesc03Subcoststr;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesp01Part;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesp04Supply;
import ar.com.ford.it.fullEconomics.persistence.dto.Teess01Supplier;
import ar.com.ford.it.fullEconomics.util.FullEconomicsDateUtil;

public class SubCostStructureDetailForm extends ValidatorForm {
    public String section;
    public String subCostStrId;
    public String tier;
    public String tierPath;
    public String description;
    public String supplierTypeSel;
    public Collection supplierTypesCol;
    public String supplierCode;
    public String lookupedSupplier;
    public String partClassSel;
    public Collection partClassesCol;
    public String partPrefix;
    public String partBase;
    public String partSufix;
    public String lookupedPart;
    public String createdBy;
    public String createdDate;
    public String lastModifiedBy;
    public String lastModifiedDate;
    public Teesc03Subcoststr subCostStr;

    public void resetForm() {
        this.section = null;
        this.subCostStrId = null;
        this.tier = null;
        this.tierPath = null;
        this.description = null;
        this.supplierTypeSel = null;
        this.supplierTypesCol = null;
        this.supplierCode = null;
        this.lookupedSupplier = null;
        this.partClassSel = null;
        this.partClassesCol = null;
        this.partPrefix = null;
        this.partBase = null;
        this.partSufix = null;
        this.lookupedPart = null;
        this.createdBy = null;
        this.createdDate = null;
        this.lastModifiedBy = null;
        this.lastModifiedDate = null;
        this.subCostStr = null;
    }

    public String getCreatedBy() {
        return createdBy;
    }

    public void setCreatedBy(String createdBy) {
        this.createdBy = createdBy;
    }

    public String getCreatedDate() {
        return createdDate;
    }

    public void setCreatedDate(String createdDate) {
        this.createdDate = createdDate;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getLastModifiedBy() {
        return lastModifiedBy;
    }

    public void setLastModifiedBy(String lastModifiedBy) {
        this.lastModifiedBy = lastModifiedBy;
    }

    public String getLastModifiedDate() {
        return lastModifiedDate;
    }

    public void setLastModifiedDate(String lastModifiedDate) {
        this.lastModifiedDate = lastModifiedDate;
    }

    public String getLookupedPart() {
        return lookupedPart;
    }

    public void setLookupedPart(String lookupedPart) {
        this.lookupedPart = lookupedPart;
    }

    public String getLookupedSupplier() {
        return lookupedSupplier;
    }

    public void setLookupedSupplier(String lookupedSupplier) {
        this.lookupedSupplier = lookupedSupplier;
    }

    public String getPartBase() {
        return partBase;
    }

    public void setPartBase(String partBase) {
        this.partBase = partBase;
    }

    public Collection getPartClassesCol() {
        return partClassesCol;
    }

    public void setPartClassesCol(Collection partClassesCol) {
        this.partClassesCol = partClassesCol;
    }

    public String getPartClassSel() {
        return partClassSel;
    }

    public void setPartClassSel(String partClassSel) {
        this.partClassSel = partClassSel;
    }

    public String getPartPrefix() {
        return partPrefix;
    }

    public void setPartPrefix(String partPrefix) {
        this.partPrefix = partPrefix;
    }

    public String getPartSufix() {
        return partSufix;
    }

    public void setPartSufix(String partSufix) {
        this.partSufix = partSufix;
    }

    public String getSubCostStrId() {
        return subCostStrId;
    }

    public void setSubCostStrId(String subCostStrId) {
        this.subCostStrId = subCostStrId;
    }

    public String getSupplierCode() {
        return supplierCode;
    }

    public void setSupplierCode(String supplierCode) {
        this.supplierCode = supplierCode;
    }

    public Collection getSupplierTypesCol() {
        return supplierTypesCol;
    }

    public void setSupplierTypesCol(Collection supplierTypesCol) {
        this.supplierTypesCol = supplierTypesCol;
    }

    public String getSupplierTypeSel() {
        return supplierTypeSel;
    }

    public void setSupplierTypeSel(String supplierTypeSel) {
        this.supplierTypeSel = supplierTypeSel;
    }

    public String getTier() {
        return tier;
    }

    public void setTier(String tier) {
        this.tier = tier;
    }

    public String getTierPath() {
        return tierPath;
    }

    public void setTierPath(String tierPath) {
        this.tierPath = tierPath;
    }

    public void setDetails(String tier, String tierPath) {
        Calendar aux = new GregorianCalendar();

        this.setSubCostStrId(this.subCostStr.getEesc03SubcstNumberK()
                .toString());
        this.setTier(tier);
        this.setTierPath(tierPath);
        this.setDescription(this.subCostStr.getEesc03SubcstNameN());
        if (this.subCostStr.getTeesp04Supply() != null) {
            this.setSupplierTypeSel(this.subCostStr.getTeesp04Supply()
                    .getTeess01Supplier().getEess01SupplierClassCodeC());
            this.setSupplierCode(this.subCostStr.getTeesp04Supply()
                    .getTeess01Supplier().getEess01SupplierCodeC());
            this.setPartClassSel(this.subCostStr.getTeesp04Supply()
                    .getTeesp01Part().getEesp01PartClassCodeC());
            this.setPartPrefix(this.subCostStr.getTeesp04Supply()
                    .getTeesp01Part().getEesp01PartPrefixCodeC());
            this.setPartBase(this.subCostStr.getTeesp04Supply()
                    .getTeesp01Part().getEesp01PartBaseCodeC());
            this.setPartSufix(this.subCostStr.getTeesp04Supply()
                    .getTeesp01Part().getEesp01PartSuffixCodeC());
        }
        this.setCreatedBy(this.subCostStr.getEesc03CreateUserC());
        this.setLastModifiedBy(this.subCostStr.getEesc03LastUpdtUserC());
        aux.setTimeInMillis(this.subCostStr.getEesc03CreateS().getTime());
        this.setCreatedDate(FullEconomicsDateUtil.format(aux));
        aux.setTimeInMillis(this.subCostStr.getEesc03LastUpdtS().getTime());
        this.setLastModifiedDate(FullEconomicsDateUtil.format(aux));
    }

    public String getSection() {
        return section;
    }

    public void setSection(String section) {
        this.section = section;
    }

    public Teesc03Subcoststr getSubCostStr() {
        return subCostStr;
    }

    public void setSubCostStr(Teesc03Subcoststr subCostStr) {
        this.subCostStr = subCostStr;
    }
    
    /**
     * 
     * @return
     */
    public Teess01Supplier getSupplier() {
        Teess01Supplier supplier = new Teess01Supplier();
        
        supplier.setEess01SupplierClassCodeC(this.getSupplierTypeSel());
        supplier.setEess01SupplierCodeC(this.getSupplierCode());
        return supplier;
    }
    
    /**
     * 
     * @return
     */
    public Teesp01Part getPart() {
        Teesp01Part part = new Teesp01Part();
        
        part.setEesp01PartClassCodeC(this.getPartClassSel());
        part.setEesp01PartPrefixCodeC(this.getPartPrefix());
        part.setEesp01PartBaseCodeC(this.getPartBase());
        part.setEesp01PartSuffixCodeC(this.getPartSufix());
        return part;
    }
    
    /**
     * 
     * @return
     */
    public Teesc03Subcoststr getSubCostStrForUpdate() {
        Teess01Supplier supplier = null;
        Teesp01Part part = null;
        Teesp04Supply supply = new Teesp04Supply();
        
        supplier = this.getSupplier();
        part = this.getPart();
        supply.setTeess01Supplier(supplier);
        supply.setTeesp01Part(part);
        this.getSubCostStr().setTeess01Supplier(supplier);
        this.getSubCostStr().setTeesp04Supply(supply);
        this.getSubCostStr().setEesc03SubcstNameN(this.getDescription());

        return this.getSubCostStr();
    }

    /**
     * 
     */
    public ActionErrors validate(ActionMapping mapping,
                                 HttpServletRequest request) {
        ActionErrors ae = new ActionErrors();
        MessageResources resources = (MessageResources) request
                .getAttribute(org.apache.struts.Globals.MESSAGES_KEY);

        if ("save".equalsIgnoreCase(this.getSection())) {
            if (StringUtils.isBlank(this.getDescription())) {
                ae
                        .add(
                                ActionMessages.GLOBAL_MESSAGE,
                                new ActionMessage(
                                        "errors.required",
                                        resources
                                                .getMessage("subCostStructure.detail.label.description")));
            }
            if (!this.getSupplierTypeSel().equals("-1")) {
                if (StringUtils.isBlank(this.getSupplierCode())) {
                    ae
                            .add(
                                    ActionMessages.GLOBAL_MESSAGE,
                                    new ActionMessage(
                                            "errors.required",
                                            resources
                                                    .getMessage("subCostStructure.detail.label.supplier")));
                }
            } else {
                if (StringUtils.isNotBlank(this.getSupplierCode())) {
                    ae
                            .add(
                                    ActionMessages.GLOBAL_MESSAGE,
                                    new ActionMessage(
                                            "errors.required",
                                            resources
                                                    .getMessage("subCostStructure.detail.label.supplierType")));
                }
            }
            if (!this.getPartClassSel().equals("-1")) {
                if (StringUtils.isBlank(this.getPartPrefix())
                        || StringUtils.isBlank(this.getPartBase())
                        || StringUtils.isBlank((this.getPartSufix()))) {
                    ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                            "errors.required", resources
                                    .getMessage("part.label")));
                }
            } else {
                if (StringUtils.isNotBlank(this.getPartPrefix())
                        || StringUtils.isNotBlank(this.getPartBase())
                        || StringUtils.isNotBlank((this.getPartSufix()))) {
                    ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                            "errors.required", resources
                                    .getMessage("part.label")));                    
                }
            }
        }

        return ae;
    }
}
